{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5b6a5d02-af4e-4e50-8b11-08a70acf7d65",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3df48d0-a6ff-47e3-a5f7-1c175048150f",
   "metadata": {},
   "source": [
    "\n",
    "## 1. 矩阵的转置(Transpose)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b87a119f-4ee3-4ce4-ab80-16828754d298",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1, 2], [3, 4]])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c47fa2da-1652-481a-8037-c0b3d5085f9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3],\n",
       "       [2, 4]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5e01cd4-bed8-4727-9dcf-6b5b7b7c7b86",
   "metadata": {},
   "source": [
    "### 3.3.2. 矩阵行列式(Determinant)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4aaee020-1ef8-4bdb-92f1-2da260578f99",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-2.0000000000000004"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[1, 2], [3, 4]])\n",
    "np.linalg.det(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78033ea0-918b-406a-8a64-65213d883e62",
   "metadata": {},
   "source": [
    "<br>\n",
    "\n",
    "### 3.3.3.  矩阵的逆(Inverse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1dcaa6fb-3ece-4150-8ee6-78b74cc78c16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[1, 2], [3, 4]])\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0a7bc840-ccdc-4cb5-90b5-dacc01b9114c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2. ,  1. ],\n",
       "       [ 1.5, -0.5]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_inv = np.linalg.inv(A)\n",
    "A_inv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8b032c5c-8226-49fa-aadb-7fe8b54ef92f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.0000000e+00, 0.0000000e+00],\n",
       "       [8.8817842e-16, 1.0000000e+00]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ A_inv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "dc6b1dc2-a4d0-4fc1-b676-90ca21f749b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.00000000e+00, 0.00000000e+00],\n",
       "       [2.22044605e-16, 1.00000000e+00]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_inv @ A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b522b845-89d7-4acf-810d-e2e1a347682a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "67fafa72-f62d-44f3-a9a5-e6b67cd594c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "I = np.diag([1, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e1d20964-fd0c-4910-947d-0e9472d546f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.allclose(A_inv @ A, I)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d40020c5-0e32-487d-9a9c-b33413a107e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.allclose(A @ A_inv , I)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16c6d5f4-d0af-4c38-b9d7-fb871926d00b",
   "metadata": {},
   "source": [
    "<br>\n",
    "\n",
    "## 方程组求解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "baec4351-4101-44d3-93cb-cae2838c7ff2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 5]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = np.array([[1, 2], [3, 5]])\n",
    "b = np.array([1, 2])\n",
    "\n",
    "display(A); display(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "86c2d83c-04b7-44b3-bc24-3d80570c58b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.,  1.])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.linalg.solve(A, b)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "21f5caf0-505a-4cb7-8c1e-7792d9f5e796",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.11022302e-16,  0.00000000e+00])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ x - b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "8c317906-855a-4d2e-a053-9d601a0768cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.allclose(A @ x, b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecc47692-362a-43fa-8fb1-f883100efe9c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7242f87b-28b7-4492-9e7f-95b30c8f8d4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1, 2], [3, 4]])\n",
    "I = np.diag([1, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c60eb802-167f-4b58-a866-af536ec6fa08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2. ,  1. ],\n",
       "       [ 1.5, -0.5]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_inv = np.linalg.solve(A, I)\n",
    "A_inv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "0c00623f-531d-4040-8211-9754cbb84602",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.0000000e+00, 0.0000000e+00],\n",
       "       [8.8817842e-16, 1.0000000e+00]])"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ A_inv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "8f9eb8ac-42bf-4839-83bc-db636d38a89f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.allclose(A @ A_inv, I)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "340c1770-7f3f-4b9e-9174-80afbc93abba",
   "metadata": {},
   "source": [
    "<br>\n",
    "\n",
    "### 3.3.5.  特征值（eigen values）、特征向量(eigen vectors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1a4da8a1-ab1f-4b81-958f-33457a401bda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[1,2,3], [4,5,6], [7,8,9]])\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "2543671c-1785-4ee7-91a9-19367a92ab97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w:\n",
      "[ 1.61168440e+01 -1.11684397e+00 -1.30367773e-15]\n",
      "v:\n",
      "[[-0.23197069 -0.78583024  0.40824829]\n",
      " [-0.52532209 -0.08675134 -0.81649658]\n",
      " [-0.8186735   0.61232756  0.40824829]]\n"
     ]
    }
   ],
   "source": [
    "w, v = np.linalg.eig(A)\n",
    "\n",
    "print(f\"w:\\n{w}\")\n",
    "print(f\"v:\\n{v}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "91b63ffd-b155-407f-8dde-53052f0597f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2., 3.],\n",
       "       [4., 5., 6.],\n",
       "       [7., 8., 9.]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v @ np.diag(w) @ np.linalg.inv(v)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0b8a13f-c8eb-4416-9a31-527dc88adb3d",
   "metadata": {},
   "source": [
    "<br>\n",
    "对角阵的特征值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "deee38ca-0fc5-4d5b-9ef3-13333f494ee6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0],\n",
       "       [0, 2, 0],\n",
       "       [0, 0, 3]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.diag([1, 2, 3])\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "518e56ee-0d72-4abf-8a0e-014f4b730bed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w:\n",
      "[1. 2. 3.]\n",
      "v:\n",
      "[[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "w, v = np.linalg.eig(A)\n",
    "\n",
    "print(f\"w:\\n{w}\")\n",
    "print(f\"v:\\n{v}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
